from moviepy.editor import VideoFileClip
from pydub import AudioSegment
import os

root_path = '/root/data/video_decoding/video'

def mp4_to_wav(name):
    mp4_file = os.path.join(root_path, 'origin', f'{name}_1.mp4')
    output_folder = os.path.join(root_path, 'split')
    video_clip = VideoFileClip(mp4_file)    # 加载 MP4 文件
    audio_clip = video_clip.audio    # 获取音频部分
    audio_duration = audio_clip.duration    # 计算音频时长（以秒为单位）
    audio_fps = audio_clip.fps

    # 分割音频并保存为 WAV 文件
    for i in range(int(audio_duration)):
        start_time = i 
        end_time = i + 1
        segment = audio_clip.subclip(start_time, end_time) 
        segment.write_audiofile(f"{output_folder}/{name}_{i+1}.wav", fps=audio_fps, nbytes=2, codec='pcm_s16le')

    print(f"已成功将 {mp4_file} 切分成 1 秒的 WAV 文件。")
    
mp4_to_wav("mj")          # fps = 44100
mp4_to_wav("porter")